import init_ai from './ai';
import { IActions, IEvents } from './interfaces';

const FD_EVENTS_INIT = 'fd-events-init';
const FD_EVENTS_WX_APP_LAUNCH = 'fd-events-wx-app-launch';
const FD_EVENTS_WX_APP_SHOW = 'fd-events-wx-app-show';
const FD_EVENTS_WX_APP_HIDE = 'fd-events-wx-app-hide';
const FD_EVENTS_WX_APP_ERROR = 'fd-events-wx-app-error';

export default function init_app(events: IEvents, actions: IActions) {
	const ai = init_ai(actions, events);
	const data = { app: null as any/* as wx.App*/ };
	const app = {
		fd: data,
		async onLaunch() {
			this.fd.app = this;
			Object.freeze(data);
			await ai.emit(this, FD_EVENTS_INIT);
			// Do something initial when launch.
			ai.emit(this, FD_EVENTS_WX_APP_LAUNCH);
		},
		onShow() {
			// Do something when show.
			ai.emit(this, FD_EVENTS_WX_APP_SHOW);
		},
		onHide() {
			// Do something when hide.
			ai.emit(this, FD_EVENTS_WX_APP_HIDE);
		},
		onError(msg: any) {
			ai.emit(this, FD_EVENTS_WX_APP_ERROR, msg);
		}
	};
	App(app);
}
